/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.freeforall;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * Controls the game and view by managing threads and touch events. 
 * @author Erik Buchholz
 * @author Mike Peterson
 */
public class GameController implements OnTouchListener{
	
	private Game game;
	private GameView gameView;
	private Vector<Thread> threadList;
	private Timer spawnTimer;
	private int currentColor = 0;
	private Object pauseObject; 
	
	/** Creates new pieces when the Timer ticks. */
	class PieceMaker extends TimerTask {
		
		GameController gc;
		
		public PieceMaker(GameController gc){
			this.gc = gc;
		}
		
		public void run(){
			if (!GameParams.getInstance().isPaused){
				int xPos = (int)(Math.random() * Game.GRID_SIZE);
				int yPos = (int)(Math.random() * Game.GRID_SIZE);
				gc.createPiece(xPos, yPos);
			}
		}
	}
	
	/** Sets attributes. */
	public GameController(Game game, GameView gameView, Object pauseObject){
		this.game = game;
		this.gameView = gameView;
		gameView.setOnTouchListener(this);
		this.pauseObject = pauseObject;
	}
	
	/** Starts the game. */
	public void start(){
		// make a piece to start.
		threadList = new Vector<Thread>();
		int xPos = (int)(Math.random() * Game.GRID_SIZE);
		int yPos = (int)(Math.random() * Game.GRID_SIZE);
		createPiece(xPos, yPos);
		Thread gameThread = new Thread(game);
		gameThread.start();
		spawnTimer = new Timer();
		PieceMaker pc = new PieceMaker(this);
		spawnTimer.schedule(pc, GameParams.getInstance().pieceSpawnTime, 
				GameParams.getInstance().pieceSpawnTime);
	}
	
	/** Create a new piece (and a thread to go with it). */
	public void createPiece(int xPos, int yPos){
		currentColor = (currentColor + 1) % 4;
		Piece newPiece = new Piece(xPos, yPos, getColor(), pauseObject);
		Game.CELL_GRID[yPos][xPos].insertPiece(newPiece);
		Thread newThread = new Thread(newPiece);
		threadList.add(newThread);
		newThread.start();
	}
	
	/** Makes new pieces on touch. */
	public boolean onTouch(View v, MotionEvent e){
		if (e.getAction() == MotionEvent.ACTION_DOWN){
			int cellX = gameView.getXCell((int)e.getX());
			int cellY = gameView.getYCell((int)e.getY());
			if (cellX > -1 && cellY > -1 && cellX < Game.GRID_SIZE && cellY < Game.GRID_SIZE){
				createPiece(cellX, cellY);
			}
		}
		return true;
	}
	
	/** Notify all threads about resume. */
	public void onReturn(){
		synchronized(pauseObject){
			pauseObject.notifyAll();
		}
		
	}
	
	/** Shut everything down. */
	public void exit(){
		GameParams.getInstance().exiting = true;
	}
	
	/** Get the color code associated with the currentColor. */
	public int getColor(){
		switch((int)(currentColor)){
		default:
		case 0:
			return 0xffffff00;
		case 1:
			return 0xffff0000;
		case 2:
			return 0xff00ff00;
		case 3:
			return 0xff0000ff;
		}
	}
}
